var LoginView = Backbone.View.extend({
	el: 'body',
	template: _.template(
		'<div class="loginForm">' +
			'<form><p class="plogin">' + 
				'<select name="login" class="login">' +
					'<% _.each(people, function(name) { %>' +
						'<option><%= name %> </option> <% }); %>' +
				'</select>' +
					'</p><input type="password" name="password" class="password">' +
			'</form>' +
			'<input id="loginButton" type="button" value="Login">' +
		'</div>'
	),
	render: function(){
		this.$el.empty();
		var self = this;
		//this.$el.html(this.template({people:['nancy','vasya', 'vicheslav dfgg', 'ppp']}));
			$.ajax({

				url: 'http://localhost:8080/org.restoran/user',
				type: 'get',
				dataType: 'json',
				success: function(data) {
					name = data && data.data;
					self.$el.html(self.template({people:_.pluck(data.data,'name')}));
				}
			});		


	},
	
	events: {
		'click input#loginButton': 'logIn'
	},
	
	logIn: function() {
		$.ajax({
			url: 'http://localhost:8080/org.restoran/login?' + $('form').serialize(),
			type: 'get',
			dataType: 'json',
			success: function(data) {
				router.apikey = data && data.data;
				if(router.apikey) {
					router.navigate("map", true);
				} else {
					alert('No such user ans password combination');
				}
			}, 
			error: function(XMLHttpRequest, textStatus, errorThrown) { 
                alert("Status: " + textStatus); alert("Error: " + errorThrown); 
            }   
			
		});
	}
});